package com.hundun.hddeal.service.lmpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hundun.hddeal.dao.NextOrderMapper;
import com.hundun.hddeal.dao.TaskOrderMapper;
import com.hundun.hddeal.entity.NextOrderPuls;
import com.hundun.hddeal.entity.TaskOrder;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.TaskOrderService;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.List;

import static org.apache.tomcat.jni.Time.now;

/**
 * 任务大厅大单业务层
 * @author 谢
 */
@Service
public class TaskOrderServiceImpl implements TaskOrderService {

    @Autowired
    private TaskOrderMapper taskOrderMapper;
    @Autowired
    private NextOrderMapper nextOrderMapper;

    @Override
    public Result deleteByPrimaryKey(Long id) {
        try {
            taskOrderMapper.deleteByPrimaryKey(id);
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result insertSelective(TaskOrder record) {
        try {
            taskOrderMapper.insertSelective(record);
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result<TaskOrder> selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public Result updateByPrimaryKeySelective(TaskOrder record) {
        try{
            record.setUpdateDate(DateUtil.now());
            taskOrderMapper.updateByPrimaryKeySelective(record);
            return ResultUtil.success();
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result<PageInfo<TaskOrder>> selectAll(Integer page, Integer size,String adType,String type,String tfPlace,Date time) {
        try{
            PageHelper.startPage(page, size);
            List<TaskOrder> TaskOrders= taskOrderMapper.selectAll(adType,type,tfPlace,time);
            for(TaskOrder taskOrder:TaskOrders){
                Base64.Decoder decoder = Base64.getDecoder();
                taskOrder.setTextBody(new String(decoder.decode(taskOrder.getTextBody()), StandardCharsets.UTF_8));
                if("阅读".equals(taskOrder.getType())){
                    //计算多出来的10%
                    double surolus=taskOrder.getAdBudget()-(taskOrder.getAdBudget()/1.1);
                    BigDecimal b = new BigDecimal(surolus);
                    surolus = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    if(taskOrder.getOutMoney()>surolus){
                      BigDecimal c = new BigDecimal(taskOrder.getOutMoney()-surolus);
                      double  outMoney = c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        taskOrder.setOutMoney(outMoney);
                    }else {
                        taskOrder.setOutMoney(0.00);
                    }

                }
            }
            PageInfo<TaskOrder> newsListPageInfo = new PageInfo<>(TaskOrders);
            return ResultUtil.success(newsListPageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    @Override
    public Result<PageInfo<TaskOrder>> selectByAdIdAndStatus(String adId, String adName, Integer status, Integer page, Integer size, String type) {
        try{
            PageHelper.startPage(page, size);
            List<TaskOrder> taskorders =taskOrderMapper.selectByAdIdAndStatus(adId,adName,status,type);
            for(int i=0;i<taskorders .size();i++){
                if(taskorders .get(i).getTextBody()!=null){
                    Base64.Decoder decoder = Base64.getDecoder();
                    taskorders .get(i).setTextBody(new String(decoder.decode(taskorders .get(i).getTextBody()), StandardCharsets.UTF_8));
                }
                //判断是否能追加费用
                if(taskorders .get(i).getTgEndtime().getTime()>System.currentTimeMillis()&&taskorders .get(i).getStatus()<4){
                    taskorders .get(i).setIsTime(true);
                }else {
                    taskorders .get(i).setIsTime(false);
                }
            }
            PageInfo<TaskOrder> newsListPageInfo = new PageInfo<>(taskorders);
            return ResultUtil.success(newsListPageInfo);
        }catch (Exception e){
         e.printStackTrace();
         return ResultUtil.error();
        }
    }


    @Override
    public Result updateOutMoney(String order, Double outMoney) {
        try{
            taskOrderMapper.updateOutMoney(order,outMoney);
            return ResultUtil.success();
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result<TaskOrder> getOne(String order, String mtId, String ghId, Long id, String type) {
        try{
            TaskOrder taskOrder=taskOrderMapper.getOne(order,type);
            //防止有两条
            NextOrderPuls nextOrderPuls= nextOrderMapper.selectGetByghId(mtId,ghId,order,id,type);
            taskOrder.setMtTextTitle(nextOrderPuls.getTextTitle());
            taskOrder.setPostUrl(nextOrderPuls.getPostUrl());
            Base64.Decoder decoder = Base64.getDecoder();
            taskOrder.setTextBody(new String(decoder.decode(taskOrder.getTextBody()), StandardCharsets.UTF_8));
            return ResultUtil.success(taskOrder);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result updateStatus(String order, int status) {
        try{
            taskOrderMapper.updateStatus(order,status);
            return ResultUtil.success();
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


}
